<template>
    <section class="mt-4">
        <h3 class="m-0">综合选号池</h3>
        <div>
            <el-input v-model="model" type="textarea" size="small" :rows="3"
                placeholder="号码之间以空格隔开，前区和后区之间以逗号隔开。示例：01 06 10 ... 35,01 02 ... 12"></el-input>
        </div>
        <div class="my-2 flex justify-between items-center">
            <el-button-group size="small">
                <el-tooltip placement="top-start" content="使用【选号池】数据和【高级筛选】数据，生成预选号码。">
                    <el-button type="info" @click="onPoolInputCommand('build-by-pool')">筛选选号池号码</el-button>
                </el-tooltip>
                <el-tooltip placement="top-start" content="使用全部 35 + 12 个号码和【高级筛选】表单数据，生成预选号码。">
                    <el-button type="info" @click="onPoolInputCommand('build-by-all')">全部号码</el-button>
                </el-tooltip>
                <el-dropdown size="small" type="info" placement="top" @command="handleHistoryCommand">
                    <el-button type="info" size="small">
                        <el-tooltip placement="top-start" content="使用历史开奖全部号码，生成预选号码。">
                            历史开奖号码
                        </el-tooltip>
                    </el-button>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item command="all">全部</el-dropdown-item>
                            <el-dropdown-item command=">|3">开奖次数大于 3 次</el-dropdown-item>
                            <el-dropdown-item command=">|5">开奖次数大于 5 次</el-dropdown-item>
                            <el-dropdown-item command=">|7">开奖次数大于 7 次</el-dropdown-item>
                            <el-dropdown-item command=">|8">开奖次数大于 8 次</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
                <el-button type="info" @click="onPoolInputCommand('invert')">反选号码生成</el-button>
            </el-button-group>
            <el-button-group size="small">
                <el-button type="info" @click="onPoolInputCommand('copy')">复制</el-button>
                <el-button type="info" @click="onPoolInputCommand('clear')">清空</el-button>
            </el-button-group>
        </div>
    </section>
</template>

<script setup>
/**
 * 综合选号池
 * 用途：菜单栏/工具/选号设置/综合选号池
 * @version 1.0.0
 */
import { ref } from 'vue';
import DltDataModel from '@/model/DltDataModel';
import ObjectUtils from '@/utils/ObjectUtils';
import { copyText } from '@/utils/Utils';
import { split_index, num_front, num_back } from '@/config/dlt-config';
import { ElMessage } from 'element-plus';

let dltDataModel = new DltDataModel();
const statDrawal = dltDataModel.getDrawalStat();
const model = defineModel();
const props = defineProps({
    columns: {
        type: Array,
        default: () => []
    }
})

const formData = ref({
    // 数据分析
    trend: '',
    sumRange: '',
    frontRange: '',
    backRange: '',
    frontIncludes: '',
    frontExcludes: '',
    backIncludes: '',
    backExcludes: '',
    area1Count: 0,
    area2Count: 0,
    area3Count: 0,
    area4Count: 0,
    area5Count: 0,
    area6Count: 0,
    area7Count: 0,
    // 选号池
    poolText: '',
    // 购物车
    cart: [],
})

const handleHistoryCommand = (command) => {
    if (command == 'all') {
        buildPoolText('history');
        return;
    }
    let arr = command.split('|');
    buildPoolText('history', { op: arr[0], value: parseInt(arr[1]) });
}
const onPoolInputCommand = (command) => {
    switch (command) {
        case 'build-by-pool':
            buildPoolText('pool');
            break;
        case 'build-by-all':
            buildPoolText('all');
            break;
        case 'invert':
            if (model.value.trim() == '') {
                model.value = num_front.join(' ') + ',' + num_back.join(' ');
                break;
            }
            let arr = model.value.split(',');
            let arr0 = arr[0].split(' ');
            let arr1 = arr[1].split(' ');
            model.value = num_front.filter(v => !arr0.includes(v)).join(' ') + ',' + num_back.filter(v => !arr1.includes(v)).join(' ');
            break;
        case 'copy':
            copyText(model.value);
            ElMessage.success('复制成功');
            break;
        case 'clear':
            model.value = '';
            break;
    }
}

/**
 * 根据【购物车】数据和【数据分析摘要】表单数据，生成预选号码。
 */
const buildPoolText = (datasourceType = 'pool', option = {}) => {
    let frontData = [], backData = [];

    if (datasourceType == 'pool') {
        // 合并 7 个号数的选号池号码
        frontData = props.columns.slice(0, split_index).flat().join(' ').split(' ').sort();
        backData = props.columns.slice(split_index).flat().join(' ').split(' ').sort();
    } else if (datasourceType == 'history') {
        frontData = ObjectUtils.statObjectToArr(statDrawal.statAll[0], false, option);
        backData = ObjectUtils.statObjectToArr(statDrawal.statAll[1], false, option);
    } else {
        frontData = dltconfig.num_front;
        backData = dltconfig.num_back;
    }
    console.log(datasourceType, frontData, backData)
    // 根据前区范围和后区范围，去头去尾
    let arr = formData.value.frontRange.trim().split('-').map(v => parseInt(v));
    if (arr > 0) {
        frontData = frontData.filter(v => parseInt(v) >= arr[0] || parseInt(v) <= arr[1]);
    }
    arr = formData.value.backRange.trim().split('-').map(v => parseInt(v));
    if (arr > 0) {
        backData = backData.filter(v => parseInt(v) >= arr[0] || parseInt(v) <= arr[1]);
    }
    // 根据前区排除和后区排除，减少号码
    if (formData.value.frontExcludes.trim().length > 0) {
        arr = formData.value.frontExcludes.trim().split(' ');
        frontData = frontData.filter(v => !arr.includes(v));
    }
    if (formData.value.backExcludes.trim().length > 0) {
        arr = formData.value.backExcludes.trim().split(' ');
        backData = backData.filter(v => !arr.includes(v));
    }
    // 根据前区包含和后区包含，增加号码（包含在排除后面，确保包含优先于排除）
    if (formData.value.frontIncludes.trim().length > 0) {
        frontData = frontData.concat(formData.value.frontIncludes.trim().split(' '));
    }
    if (formData.value.backIncludes.trim().length > 0) {
        backData = backData.concat(formData.value.backIncludes.trim().split(' '));
    }
    // 去重并排序
    frontData = [...new Set(frontData)].sort();
    backData = [...new Set(backData)].sort();
    model.value = frontData.join(' ') + ', ' + backData.join(' ');

}
</script>